package iterator

import "fmt"

type Aggregate interface {
	Iterator() Iterator
}
type Iterator interface {
	First()
	IsDone() bool
	Next() interface{}
}
type Numbers struct {
	start, end int
}

type Strings struct {
	c     string
	start int
	end   int
}

func NewStrings(c string) *Strings {
	return &Strings{c: c, start: 0, end: len(c) - 1}
}

func (s *Strings) Iterator() Iterator {
	return &StringIterator{
		s:     s,
		index: s.start,
	}
}

type StringIterator struct {
	s     *Strings
	index int
}

func (i *StringIterator) First() {
	i.index = i.s.start
}
func (i *StringIterator) IsDone() bool {
	return i.index > i.s.end
}
func (i *StringIterator) Next() interface{} {
	if !i.IsDone() {
		next := i.s.c[i.index]
		i.index++
		return string(next)
	}
	return ""
}

func NewNumbers(start int, end int) *Numbers {
	return &Numbers{start: start, end: end}
}
func (n *Numbers) Iterator() Iterator {
	return &NumbersIterator{
		numbers: n,
		next:    n.start,
	}
}

type NumbersIterator struct {
	numbers *Numbers
	next    int
}

func (i *NumbersIterator) First() {
	//第一个 i.numbers.start
	i.next = i.numbers.start
}
func (i *NumbersIterator) IsDone() bool {
	return i.next > i.numbers.end
}
func (i *NumbersIterator) Next() interface{} {
	if !i.IsDone() {
		next := i.next
		i.next++
		return next
	}
	return nil
}

// i.First  i.isone
// netx
// c
//
func IteratorPrint(i Iterator) {
	for i.First(); !i.IsDone(); {
		c := i.Next()
		fmt.Printf("%#v", c)
		fmt.Println()
	}
}
